package com.tianxiang.dao;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

import com.tianxiang.hibernatesession.HibernateSessionFactory;
import com.tianxiang.model.MyFile;
import com.tianxiang.model.Owner;
import com.tianxiang.model.TmpFile;

public class MyFileDao {

	Session getSession(){
		return HibernateSessionFactory.getSession();
	}
	@SuppressWarnings("unchecked")
	public List<MyFile> findByProperty(String propertyName, Object value) {
		try {
			String queryString = "from MyFile as s where s." + propertyName
					+ "=?";
			Query queryObject = getSession().createQuery(queryString);
			queryObject.setParameter(0, value);
			return queryObject.list();
		} catch (HibernateException e) {
			throw e;
		}
	}
	/**
	 * 根据id获取文件实例实例
	 * @param fileCode
	 * @return
	 */
	public MyFile findFileById(int id){
		Query queryObject = getSession().createQuery("from MyFile as s where s.id=?");
		queryObject.setParameter(0, id);
		@SuppressWarnings("unchecked")
		List<MyFile> list=queryObject.list();
		if(list.size()!=0){
			return list.get(0);
		}else{
			return null;
		}
	}
	/**
	 * 根据fileCode获取文件实例实例
	 * @param fileCode
	 * @return
	 */
	public MyFile findByFileCode(String fileCode){
		Query queryObject = getSession().createQuery("from MyFile where fileCode=?");
		queryObject.setParameter(0, fileCode);
		@SuppressWarnings("unchecked")
		List<MyFile> list=queryObject.list();
		System.out.println("MyFile的文件个数："+list.size());
		if(list.size()==0){
			return null;
		}else{
			return list.get(0);
		}
	}
	/**
	 * 根据用户名获取用户的文件
	 * @param fileCode
	 * @return
	 */
	public ArrayList<Owner> findUserFile(String username){
		Query queryObject = getSession().createQuery("from Owner where username=?");
		queryObject.setParameter(0, username);
		ArrayList<Owner> lists=(ArrayList<Owner>)queryObject.list();
		System.out.println(lists.size());
		if(lists.size()!=0){
			return lists;
		}else{
			return null;
		}
	}
	/**
	 * 根据用户名获取单个文件
	 * @param fileCode
	 * @return
	 */
	public Owner findOwnerFile(String username,String fileName){
		Query queryObject = getSession().createQuery("from Owner as s where s.username=? and s.ofileName=?");
		queryObject.setParameter(0, username);
		queryObject.setParameter(1, fileName);
		ArrayList<Owner> lists=(ArrayList<Owner>)queryObject.list();
		if(lists.size()!=0){
			return lists.get(0);
		}else{
			return null;
		}
	}
	/**
	 * 保存用户单例文件信息
	 * 
	 * @param myFile
	 */
	public boolean saveOwner(Owner o) {
		try {
			Transaction transaction = getSession().beginTransaction();
			transaction.begin();// 事务开始
			getSession().save(o);
			transaction.commit();// 提交事务
		} catch (RuntimeException e) {
			e.printStackTrace();
			return false;
		}
		return true;
	}
	/**
	 * 保存对象 描述方法的功能
	 * 
	 * @param myFile
	 */
	public boolean save(File f,String fileName,String fileType,String code,String path) {
		
		MyFile myFile=new MyFile();
		myFile.setName(fileName);
		myFile.setFileType(fileType);
		myFile.setFileSize(Long.toString(f.length()));
		myFile.setPath(path);
		myFile.setFileCode(code);
		try {
			Transaction transaction = getSession().beginTransaction();
			transaction.begin();// 事务开始
			getSession().save(myFile);
			transaction.commit();// 提交事务
		} catch (RuntimeException e) {
			e.printStackTrace();
			return false;
		}
		return true;
	}
	/**
	 * 更新Owner
	 * @param file
	 * @return
	 */
	public boolean updateOwner(Owner o) {
		try {
			Transaction transaction = getSession().beginTransaction();
			transaction.begin();// 事务开始
			getSession().update(o);
			transaction.commit();// 提交事务
		} catch (HibernateException e) {
			return false;
		}
		return true;
	}
	/**
	 * 更新MyFile
	 * @param file
	 * @return
	 */
	public boolean update(MyFile file) {
		try {
			Transaction transaction = getSession().beginTransaction();
			transaction.begin();// 事务开始
			getSession().update(file);
			transaction.commit();// 提交事务
		} catch (HibernateException e) {
			return false;
		}
		return true;
	}
	/**
	 * 删除Owner记录
	 * @param id
	 * @return
	 */
	public boolean deOwner(int id){
		Transaction transaction=getSession().beginTransaction();
		transaction.begin();
		String sqlString="delete Owner where id="+id;
		Query query=getSession().createQuery(sqlString);
		query.executeUpdate();
		transaction.commit();
		return true;
	}
	/**
	 * 删除记录
	 * @param id
	 * @return
	 */
	public boolean delFile(int id){
		Transaction transaction=getSession().beginTransaction();
		transaction.begin();
		String sqlString="delete MyFile where id="+id;
		Query query=getSession().createQuery(sqlString);
		query.executeUpdate();
		transaction.commit();
		return true;
	}
}
